﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using HostTool.Core;
using HostTool.Protocol;
using System.Threading;

namespace HostTool.GUI
{
    public partial class FormVersion : Form
    {

        List<CPUVersion> listCPU = new List<CPUVersion>();

        Thread threadQuery;


        bool disposing = false;

        public FormVersion()
        {
            InitializeComponent();

            HostMachine machine = MachineManager.GetMachine();


            foreach (IBoard board in machine.Boards)
            {
                foreach (ICPU cpu in board.CPUS)
                {
                    listCPU.Add(new CPUVersion(cpu as CPU));
                }
            }


            ultraGrid1.DisplayLayout.GroupByBox.Hidden = true;
           
            ultraGrid1.DisplayLayout.Override.RowSelectors = Infragistics.Win.DefaultableBoolean.True;
            ultraGrid1.DisplayLayout.Override.RowSelectorHeaderStyle = Infragistics.Win.UltraWinGrid.RowSelectorHeaderStyle.ColumnChooserButton;
            ultraGrid1.DisplayLayout.Override.RowSelectorStyle = Infragistics.Win.HeaderStyle.WindowsXPCommand;


            ultraGrid1.DisplayLayout.Bands[0].Columns.Add("BoardName", "板卡名称").Width = 100;
            ultraGrid1.DisplayLayout.Bands[0].Columns.Add("CPUName", "CPU名称").Width = 100;
            ultraGrid1.DisplayLayout.Bands[0].Columns.Add("BIOSDate", "BIOS日期").Width = 120;
            ultraGrid1.DisplayLayout.Bands[0].Columns.Add("BIOSVersion", "BIOS版本号").Width = 120;
            ultraGrid1.DisplayLayout.Bands[0].Columns.Add("APPDate", "APP日期").Width = 120;
            ultraGrid1.DisplayLayout.Bands[0].Columns.Add("APPVersion", "APP版本号").Width = 120;
            ultraGrid1.DisplayLayout.Bands[0].Columns.Add("USBDate", "USB日期").Width = 120;
            ultraGrid1.DisplayLayout.Bands[0].Columns.Add("USBVersion", "USB版本号").Width = 120;
            ultraGrid1.DataSource = listCPU;

        }


        private void ProcQueryVersion()
        {
            try
            {
                for (int i = 0; i < listCPU.Count; i++)
                {
                    listCPU[i].Clear();
                    this.Invoke(new MethodInvoker(delegate
                    {
                        ultraGrid1.Invalidate();
                    }));
                    listCPU[i].Update();
                    this.Invoke(new MethodInvoker(delegate
                    {
                        ultraGrid1.Invalidate();
                    }));
                    
                }
            }
            catch (Exception) { }
            finally
            {
                if (this.disposing == false)
                {
                    this.Invoke(new MethodInvoker(delegate
                    {
                        btQuery.Enabled = true;
                    }));
                }
            }
            
        }

        private void StartQuery()
        {
            btQuery.Enabled = false;
            if (threadQuery == null || threadQuery.IsAlive == false)
            {
                threadQuery = new Thread(new ThreadStart(ProcQueryVersion));
                threadQuery.IsBackground = true;
                threadQuery.Start();
            }
        }

        private void StopQuery(bool dispose)
        {
            this.disposing = dispose;
            if (threadQuery != null && threadQuery.IsAlive)
            {
                threadQuery.Abort();
            }

            btQuery.Enabled = true;
        }

        private void btQuit_Click(object sender, EventArgs e)
        {
            StopQuery(true);
            this.Close();

           
        }

        private void btQuery_Click(object sender, EventArgs e)
        {
            StartQuery();
        }
    }

    class CPUVersion
    {
        CPU cpu;

        public CPUVersion(CPU cpu)
        {
            this.cpu = cpu;
            
        }

        public string BoardName
        {
            get {
               return cpu.Board.Name;
            }
        }

        public string CPUName
        {
            get {
                return cpu.Name;
            }
        }

        public string BIOSDate
        {
            get;
            private set;

        }
        public string BIOSVersion
        {
            get;
            private set;
        }

        public string APPDate
        {
            get;
            private set;
        }
        public string APPVersion
        {
            get;
            private set;
        }

        public string USBDate
        {
            get;
            private set;
        }
        public string USBVersion
        {
            get;
            private set;
        }

        public void Clear()
        {
            this.BIOSDate = "";
            this.BIOSVersion = "";
            this.APPDate = "";
            this.APPVersion = "";
            this.USBDate = "";
            this.USBVersion = "";
        }

        public void Update()
        {
            RequestPackage package = cpu.CreateRequest(0x03, 0x27, 2000); //发送0x27返回0x28
            cpu.SendRequest(package);
            Frame frame = package.WaitResponse(); //等待2秒
            if (frame != null)
            {
                int N = frame.RealDataLength / 7;
                byte[] data = frame.RawData;
                int offset = frame.RealDataOffset;
                for (int i = 0; i < N; i++)
                {
                    //版本
                    StringBuilder sbVer = new StringBuilder("V");
                    string vers = data[offset + i * 7 + 1].ToString("X2");

                    sbVer.Append(vers[0] + "." + vers[1] + ".");
                    vers = data[offset + i * 7 + 2].ToString("X2");
                    sbVer.Append(vers[0] + "." + vers[1]);
                    StringBuilder sbTime = new StringBuilder();

                    sbTime.Append(data[offset + i * 7 + 3].ToString("X2"));
                    sbTime.Append(data[offset + i * 7 + 4].ToString("X2"));
                    sbTime.Append("年");


                    sbTime.Append(data[offset + i * 7 + 5].ToString("X2"));
                    sbTime.Append("月");
                    sbTime.Append(data[offset + i * 7 + 6].ToString("X2"));
                    sbTime.Append("日");
                    switch (data[offset + i * 7])
                    {
                        case 0:
                            this.BIOSVersion = sbVer.ToString();
                            this.BIOSDate = sbTime.ToString();
                            break;
                        case 1:
                            this.APPVersion = sbVer.ToString();
                            this.APPDate = sbTime.ToString();
                            break;
                        case 2:
                            this.USBVersion = sbVer.ToString();
                            this.USBDate = sbTime.ToString();
                            break;
                    }


                }
            }
            else
            {
                this.BIOSDate = "???";
                this.BIOSVersion = "???";
                this.APPDate = "???";
                this.APPVersion = "???";
            }
        }
    }
}
